<%--
  Created by IntelliJ IDEA.
  User: admin
  Date: 2020/4/1
  Time: 11:24
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<html>
<head>
    <title>修改信息</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- 引入 Bootstrap -->
    <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
    <div class="row clearfix">
        <div class="col-md-12 column">
            <div class="page-header">
                <h1 align="center">
                    员工数据管理系统
                </h1>
            </div>
        </div>
    </div>

    <div class="row clearfix">
        <div class="col-md-12 column">
            <div class="page-header">
                <h1>
                    <small>修改员工</small>
                </h1>
            </div>
        </div>
    </div>

    <form action="" name="userForm">
        <input type="hidden" name="id" value="${worker.id}"/>
        姓名：<input type="text" name="name" id="ad_name" value="${worker.name}" oninput="resName()"/><span id="ad_sname"></span><br><br><br>
        性别：<input type="text" name="sex" id="ad_sex" value="${worker.sex}" oninput="resSex()"/><span id="ad_ssex"></span><br><br><br>
        籍贯：<input type="text" name="birthplace" id="ad_birthplace" value="${worker.birthplace}" oninput="resBirthplace()"/><span id="ad_sbirthplace"></span><br><br><br>
        学历：<input type="text" name="education" id="ad_education" value="${worker.education}" oninput="resEducation()"/><span id="ad_seducation"></span><br><br><br>
        地址：<input type="text" name="address" id="ad_address" value="${worker.address}" oninput="resAddress()"/><span id="ad_saddress"></span><br><br><br>
        电话：<input type="text" name="tele" id="ad_tele" value="${worker.tele}" oninput="resTele()"/><span id="ad_stele"></span><br><br><br>
        工龄：<input type="text" name="workage" id="ad_workage" value="${worker.workage}" oninput="resWorkage()" /><span id="ad_sworkage"></span><br><br><br>
        基本工资：<input type="text" name="salary" id="ad_salary" value="${worker.salary}" oninput="resSalary()"/><span id="ad_ssalary"></span><br><br><br>
        上一次修改：<input type="text" name="update_info" id="addfd" value="${worker.upTime}" disabled="disabled"/><br><br><br>
        <input type="button" value="提交" id="btnUpdate" onclick="updateWorker()"/>
    </form>
    <script type="text/javascript">
        function updateWorker() {
            var form = document.forms[0];
            form.action = "<%=basePath %>/worker/updateWorker";
            form.method = "post";
            form.submit();
        }
    </script>
    <script type="text/javascript">
        function addWorker() {
            var form = document.forms[0];
            form.action = "<%=basePath %>worker/addWorker";
            form.method = "post";
            form.submit();
        }
        // 0表示无法进行添加,1表示可以添加
        var flag_name = 0;
        var flag_sex = 0;
        var flag_education = 0;
        var flag_birthplace = 0;
        var flag_address = 0;
        var flag_tele = 0;
        var flag_workage = 0;
        var flag_salary = 0;

        // function restraint()
        // {
        //     document.getElementById("btnCommit").disable = !(resName() === 1 && resSex() === 1 && resEducation() === 1);
        //     if ((20 > document.getElementById("name").value.length > 0 ) && (document.getElementById("sex").value === "1" || document.getElementById("sex").value === "0") && (document.getElementById("education").value === "0" || document.getElementById("education").value === "1" || document.getElementById("education").value === "2" || document.getElementById("education").value === "3" || document.getElementById("education").value === "4"))
        //     {
        //         document.getElementById("btnCommit").disabled = false;
        //     }
        // }


        //name 0<表示长度<=20
        function resName() {
            if (document.getElementById("ad_name").value.length === 0 || document.getElementById("ad_name").value.length > 20 || document.getElementById("ad_name").value.match(" "))
            {
                document.getElementById("ad_sname").innerText = "姓名长度必须大于零或小于20！";
                document.getElementById("btnUpdate").disabled = true;
            }
            else
            {
                document.getElementById("ad_sname").innerText = "";
                flag_name = 1;
                document.getElementById("btnUpdate").disabled = !(flag_name === 1);
            }
            return flag_name;
        }

        //sex 0(女) or 1（男）
        function resSex() {
            if (document.getElementById("ad_sex").value === "1" || document.getElementById("ad_sex").value === "0")
            {
                document.getElementById("ad_ssex").innerText = "";
                flag_sex = 1;
                document.getElementById("btnUpdate").disabled = !(flag_sex === 1);            }
            else
            {
                document.getElementById("ad_ssex").innerText = "请用使用0或1表示女或男！";
                document.getElementById("btnUpdate").disabled = true;
            }
            return flag_sex;
        }

        //学历 0高中1学士2硕士3博士4其它
        function resEducation() {
            if (document.getElementById("ad_education").value === "0" || document.getElementById("ad_education").value === "1" || document.getElementById("ad_education").value === "2" || document.getElementById("ad_education").value === "3" || document.getElementById("ad_education").value === "4")
            {
                document.getElementById("ad_seducation").innerText = "";
                flag_education = 1;
                document.getElementById("btnUpdate").disabled = !(flag_education === 1);            }
            else
            {
                document.getElementById("ad_seducation").innerText = "请用使用0、1、2、3、4分别表示高中、学士、硕士、博士、其他学位！";
                document.getElementById("btnUpdate").disabled = true;
            }
            return flag_education;
        }

        //籍贯 0<表示长度<=10
        function resBirthplace() {
            if ( document.getElementById("ad_birthplace").value.length === 0 || document.getElementById("ad_birthplace").value.length > 10)
            {
                document.getElementById("ad_sbirthplace").innerText = "籍贯长度必须大于零或小于10！";
                document.getElementById("btnUpdate").disabled = true;
            }
            else
            {
                document.getElementById("ad_sbirthplace").innerText = "";
                flag_birthplace = 1;
                document.getElementById("btnUpdate").disabled = !(flag_birthplace === 1);            }
            return flag_birthplace;
        }

        //地址0<表示长度<=30
        function resAddress() {
            if(document.getElementById("ad_address").value.length ===0 || document.getElementById("ad_address").value.length > 30)
            {
                document.getElementById("ad_saddress").innerText = "地址长度必须大于零或小于30！";
                document.getElementById("btnUpdate").disabled = true;
            }
            else
            {
                document.getElementById("ad_saddress").innerText = "";
                flag_address = 1;
                document.getElementById("btnUpdate").disabled = !(flag_address === 1);
            }
            return flag_address;
        }

        //电话 0<表示长度<=15，有分机时在后面使用’-’接分机号
        function resTele() {
            var integer1 = /^((\d{8})|(0\d{2,3}-\d{8,9})|(1[34578]\d{9}))$/;
            if(!integer1.test(document.getElementById("ad_tele").value) || document.getElementById("ad_tele").value.length===0)
            {
                document.getElementById("ad_stele").innerText = "长度不正确或电话号无效";
                document.getElementById("btnUpdate").disabled = true;
            }
            else
            {
                document.getElementById("ad_stele").innerText = "";
                flag_tele = 1;
                document.getElementById("btnUpdate").disabled = !(flag_tele === 1);
            }
            return flag_tele;
        }

        //工龄 0<工龄<50
        function resWorkage() {
            var a = document.getElementById("ad_workage").value;
            var workage = parseInt(a);
            if ( workage <= 0 || workage > 50 || document.getElementById("ad_workage").value.length ===0 || parseInt(a).toString() === "NaN" || a.match(" ") || document.getElementById("ad_workage").value.length > 2 || (parseInt(a.substr(1)).toString() === "NaN" && a.length===2))
            {
                document.getElementById("ad_sworkage").innerText = "工龄必须大于零小于50,且必须输入数字！";
                document.getElementById("btnUpdate").disabled = true;
            }
            else
            {
                document.getElementById("ad_sworkage").innerText = "";
                flag_workage = 1;
                document.getElementById("btnUpdate").disabled = !(flag_workage === 1);
            }
            return flag_workage;
        }

        //基本工资 0<工资
        function resSalary() {
            var a = document.getElementById("ad_salary").value;
            var salary = parseFloat(a);
            var res = /^(-)?\d+(\.\d+)?$/;
            if (salary <= 0 || document.getElementById("ad_salary").value.length === 0 || parseFloat(a).toString() === "NaN" || a.match(" "))
            {
                document.getElementById("ad_ssalary").innerText = "工资必须大于零！且只能是数字！";
                document.getElementById("btnUpdate").disabled = true;
            }
            else
            {
                document.getElementById("ad_ssalary").innerText = "";
                flag_salary = 1;
                document.getElementById("btnUpdate").disabled = !(flag_salary === 1);
            }
        }

    </script>
</div>